﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enring;
using Enring.Data;
using System.Data;
using System.Data.SqlClient;

namespace Web.Areas.Admin.Controllers
{
    public class SettingsController : BaseController
    {
        //
        // GET: /Admin/Settings/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Upload comment
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadComment()
        {
            string title = HttpContext.Request["content"] != null ? HttpContext.Request["content"] : "";
            if (string.IsNullOrEmpty(title))
                return GetResult(CODE_ERROR_NODATAFOUND, UI.Localize("No content found"));
            //Get other informations
            string deviceid = HttpContext.Request["deviceid"] != null ? HttpContext.Request["deviceid"] : "";
            string userid = HttpContext.Request["userid"] != null ? HttpContext.Request["userid"] : EnringContext.Current.CurrentUserID;
            string ip = HttpContext.Request.UserHostAddress;

            string sql = string.Format("INSERT INTO SYS_Feedbacks ([Description],[CreatedTime],[IP],[UserID],[DeviceID]) VALUES (@Description,@CreatedTime,@IP,@UserID,@DeviceID)");
            
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("Description", title));
            parameters.Add(new SqlParameter("CreatedTime", DateTime.Now.ToString()));
            parameters.Add(new SqlParameter("IP", ip));
            parameters.Add(new SqlParameter("UserID", userid));
            parameters.Add(new SqlParameter("DeviceID", deviceid));

            if (DB.ExecuteNonQuery(DB.ConnectionStringFoundation, System.Data.CommandType.Text, sql, parameters.ToArray()) > 0)
                return GetResult(CODE_SUCCESS, UI.Localize("Upload successfully"));
            else
                return GetResult(CODE_ERROR_EXECUTE,UI.Localize("Upload fail."));
        }

        /// <summary>
        /// Get software component settings
        /// </summary>
        /// <param name="code">component code. e.g. xxxclient</param>
        /// <returns></returns>
        public ActionResult GetSettings(string code)
        {
            Dictionary<string, string> settings = new Dictionary<string, string>();
            settings.Add("version", "1.0");
            settings.Add("name", "动车组便携式故障处理系统");
            settings.Add("pagecount", "7");
            return Json(settings);
        }

        /// <summary>
        /// Check update configration
        /// </summary>
        /// <param name="code">software code. e.g. tsxpad</param>
        /// <param name="version">reserved</param>
        /// <returns></returns>
        public ActionResult CheckUpdate(string code, string version)
        {
            string sql = "SELECT * FROM SYS_SoftwareComponents WHERE Name = @Name";
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("Name", code));
            DataSet ds = DB.GetDataSet(DB.ConnectionStringFoundation, System.Data.CommandType.Text, sql, parameters.ToArray());
            if (DB.HasData(ds))
            {
                Dictionary<string, string> kvs = new Dictionary<string, string>();
                DataRow row = ds.Tables[0].Rows[0];
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    kvs.Add(col.ColumnName,row[col.ColumnName].ToString());
                }
                return GetResult(CODE_SUCCESS, UI.Localize("Check software component successfully"),kvs);
            }
            else
                return GetResult(CODE_ERROR_NODATAFOUND, UI.Localize("No software component found."));
        }
    }
}
